package com.example.demo.Controller;

import cn.hutool.json.JSONObject;
import com.example.demo.Entity.Comment;
import com.example.demo.Service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户评论信息前端操作接口
 **/
@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentService commentService;

    /** 新增评论 */
    @PostMapping("/insert")
    public void insertUser(@RequestBody Comment comment) {
        commentService.insertComment(comment);
    }

    /** 根据评论ID查询评论 */
    @GetMapping("/selectById/{id}")
    public List<JSONObject> findById(@PathVariable Integer id) {
        return commentService.selectById(id);
    }

    /** 根据用户ID查询评论 */
    @GetMapping("/selectByUserId/{id}")
    public List<JSONObject> findByUserId(@PathVariable Integer id) {
        return commentService.selectByUserId(id);
    }

    /** 删除评论 */
    @GetMapping("/deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer id) {
        commentService.deleteById(id);
    }

    /** 更新评论 */
    @PostMapping("/update")
    public void update(@RequestBody Comment comment) {
        commentService.update(comment);
    }

    /** 评论点赞 */
    @PostMapping("/upvote")
    public void upvote(@RequestParam Integer commId) {
        commentService.upvote(commId);
    }

    /** 评论取消点赞 */
    @PostMapping("/cancelUpvote")
    public void cancelUpvote(@RequestParam Integer commId) {
        commentService.cancelUpvote(commId);
    }

    /** 得到笔记评论数 */
    @GetMapping("/getCommentNum")
    public List<JSONObject> getCommentNum(@RequestParam Integer userId){
        return commentService.getCommentNum(userId);
    }
}
